'use strict';
var AKONG = window.AKONG || {};
AKONG.Loading = function() {

}
AKONG.Loading.prototype = {
	constructor: AKONG.Loading,
	init: function(object) {
		var self = this;
		self.count = 0;
		self.total = 0;
		self.progress = object.progress || false;
		self.callback = object.callback || false;

		self.resources = {
			images: {
				paths: [], //路径字符串的资源
				imgs: document.images, //img标签的资源
				bgs: [], //背景图的资源
				imgs_name: 0,
				bgs_name: 0
			},
			audios: {
				paths: [], //路径字符串的资源
				audios: [] //audio标签的资源
			}
		}
		if(object.resources) {
			var images = object.resources.images,
				audios = object.resources.audios;
			if(images) {
				images.paths && (self.resources.images.paths = images.paths);
				images.imgs && (self.resources.images.imgs = images.imgs);
				images.bgs && (self.resources.images.bgs = images.bgs);
				images.imgs_name && (self.resources.images.imgs_name = images.imgs_name);
				images.bgs_name && (self.resources.images.bgs_name = images.bgs_name);
			}
			if(audios) {
				audios.paths && (self.resources.audios.paths = audios.paths);
				audios.audios && (self.resources.audios.audios = audios.audios);
			}
		};

		//筛选资源
		var images = self.resources.images,
			audios = self.resources.audios;
		//路径字符串的资源
		(function() {
			var paths = self.resources.images.paths,
				l = paths.length;
			for(var i = 0; i < l; i++) {
				var image = new Image();
				image.src = paths[i];
				paths[i] = image;
			}
			self.total += l;
		})();
		//标签的资源
		(function() {
			var imgs = self.resources.images.imgs,
				imgs_arr = [],
				l = imgs.length;
			for(var i = 0; i < l; i++) {
				var image = imgs[i],
					src = image.getAttribute(images.imgs_name || 'src');
				if(src) {
					image.src = src;
					imgs_arr.push(image);
					images.imgs_name && image.removeAttribute(images.imgs_name);
				}
			}
			self.total += imgs_arr.length;
			self.resources.images.imgs = imgs_arr;
		})();
		//背景图的资源
		(function() {
			var bgs = self.resources.images.bgs,
				bgs_arr = [],
				l = bgs.length,
				s = !!window.getComputedStyle;
			for(var i = 0; i < l; i++) {
				var background = bgs[i],
					url = '';
				if(images.bgs_name) {
					url = background.getAttribute(images.bgs_name);
					if(url){
						background.removeAttribute(images.bgs_name);
						background.style.backgroundImage = 'url('+url+')';
					}
				} else {
					url = s ? window.getComputedStyle(background).backgroundImage : background.currentStyle.backgroundImage;
					if(url !== 'none') {
						url = url.slice(4, url.length - 1);
						url[0] === '"' && (url = url.slice(1,url.length - 1));
					}
				}
				if(url && url !== 'none') {
					var image = new Image();
					image.src = url;
					bgs_arr.push(image);
				}
			}
			self.total += bgs_arr.length;
			self.resources.images.bgs = bgs_arr;
		})();

		self.total ? self.load() : (self.callback && self.callback());
	},
	load: function() { //加载
		var self = this,
			images = self.resources.images,
			audios = self.resources.audios;
		for(var i in images) {
			(function(v) {
				if(v instanceof Array) {
					var l = v.length;
					for(var j = 0; j < l; j++) {
						(function(image) {
							if(image.complete) {
								self.loadEnd(image.src);
							} else {
								image.onload = function() {
									image.onload = null;
									self.loadEnd(image.src);
								}
								image.onerror = function() {
									image.onerror = null;
									self.loadEnd(image.src);
								}
							}
						})(v[j]);
					}
				}
			})(images[i]);
		}
	},
	loadEnd: function(src) {
		var self = this,
			per;
		self.count++;
		per = self.count / self.total;
		self.progress && self.progress(per, src);
		if(per === 1) {
			self.callback && self.callback();
		}
	}
}